package org.dna.wicketui;

import org.dna.metronomo.persistence.User;
import org.tigris.scarab.model.Module;
import junit.framework.TestCase;

import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.dna.MetroApplicationTestFacility;
import org.dna.metronomo.persistence.IUserDAO;
import org.dna.metronomo.persistence.IModuleDAO;
import static org.mockito.Mockito.*;

/**
 * Base class for all metronomo related test units.
 */
public class MetroTestCase extends TestCase {
    
    public static final String ADMIN_SHA1 = "0DPiKuNIrrVmD8IUCuw1hQxNqZc=";

    protected WicketTester tester;
    protected MetroApplicationTestFacility app;

    @Override
    public void setUp() {
        app = new MetroApplicationTestFacility();
	moduleDaoFixture();
        tester = new WicketTester(app);
    }

    protected void userDaoWithAdminFixture() {
        IUserDAO userDAOMock = mock(IUserDAO.class);
        User adminUser = new User("admin", ADMIN_SHA1, "admin");
        when(userDAOMock.findUser(eq("admin"), eq(ADMIN_SHA1))).thenReturn(adminUser);
        app.setUserDAO(userDAOMock);
    }

    protected void moduleDaoFixture() {
        IModuleDAO moduleDAOMock = mock(IModuleDAO.class);
	Module defaultModule = mock(Module.class);
	when(defaultModule.getModuleId()).thenReturn(0);
	app.setModuleDAO(moduleDAOMock);
    }

    protected void doLoginAdmin() {
        // assert rendered page class
        tester.assertRenderedPage(LoginPage.class);

        FormTester loginFormTester = tester.newFormTester("form");
        loginFormTester.setValue("metroUsername", "admin");
        loginFormTester.setValue("metroPassword", "admin");
        loginFormTester.submit();
    }
}
